package com.bkgy.modularbx.processrule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.modularbx.processrule.domain.ProcessRuleLibrary;
import com.bkgy.modularbx.processrule.dto.ProcessRuleLibraryDto;
import com.bkgy.modularbx.processrule.dto.ProcessRuleLibraryUpdateDto;
import com.bkgy.modularbx.processrule.service.ProcessRuleLibraryService;
import com.bkgy.sys.common.entity.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * 工艺规则库控制器
 *
 * @author trae
 */
@Tag(name = "工艺规则库管理", description = "工艺规则库接口")
@RestController
@RequestMapping("/processRule")
@Validated
public class ProcessRuleLibraryController {

    @Resource
    private ProcessRuleLibraryService processRuleLibraryService;

    @Operation(summary = "新增工艺规则", description = "添加新的工艺规则记录")
    @PostMapping("/add")
    public ResultData<Void> add(@Parameter(description = "工艺规则信息") @RequestBody @Valid ProcessRuleLibrary processRuleLibrary) {
        return processRuleLibraryService.addProcessRule(processRuleLibrary);
    }

    @Operation(summary = "分页查询工艺规则", description = "根据条件分页查询工艺规则信息")
    @PostMapping("/page")
    //todo 页面条件少一个初始变量（之前查询也是查询2个）
    public ResultData<Page<ProcessRuleLibrary>> page(@Parameter(description = "查询条件和分页信息") @RequestBody @Valid QueryDTO<ProcessRuleLibraryDto> queryDTO) {
        return processRuleLibraryService.pageProcessRule(queryDTO);
    }

    @Operation(summary = "删除工艺规则", description = "根据ID删除工艺规则记录")
    @GetMapping("/delete")
    public ResultData<Void> delete(@Parameter(description = "删除条件") @RequestParam("processRuleVariable") String processRuleVariable) {
        return processRuleLibraryService.deleteProcessRule(processRuleVariable);
    }

    @Operation(summary = "修改工艺规则", description = "更新工艺规则信息")
    @PostMapping("/update")
    public ResultData<Void> update(@Parameter(description = "更新的工艺规则信息") @RequestBody @Valid ProcessRuleLibraryUpdateDto dto) {
        ProcessRuleLibrary processRuleLibrary = new ProcessRuleLibrary();
        BeanUtils.copyProperties(dto, processRuleLibrary);
        return processRuleLibraryService.updateProcessRule(processRuleLibrary);
    }

    @Operation(summary = "查询工艺规则详情", description = "根据主键查询工艺规则详细信息")
    @PostMapping("/findById")
    public ResultData<ProcessRuleLibrary> findById(@Parameter(description = "工艺规则变量") @RequestBody @Valid @NotBlank(message = "processRuleVariable不可以为空") String processRuleVariable) {
        return processRuleLibraryService.findById(processRuleVariable);
    }
}